package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.common.result.PageResult;
import com.tsing.mapper.ArticleMapper;
import com.tsing.pojo.entity.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/8 11:36
 */
public class ArticleMapperImpl extends CommonDao implements ArticleMapper {

    @Override
    public PageResult<TlPost> list(Integer channelId,Integer page,Integer size) {
        int offset = (page - 1) * size;

        PageResult<TlPost> result = new PageResult<>();

        StringBuilder sql = new StringBuilder("SELECT * FROM tl_post where channelId = ?  order by " +
                "createDate desc" +
                " " +
                "limit ?,?");

        List<Object> paramsCount = new ArrayList<>();
        paramsCount.add(channelId);
        paramsCount.add(offset);
        paramsCount.add(size);



        System.out.println("Executing query: " + sql);
        System.out.println("With parameters: " + Arrays.toString(paramsCount.toArray()));

        List<TlPost> wmNews = null;

        try {
            wmNews = executeQuery(TlPost.class, sql.toString(), paramsCount.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        try {
            result.setTotal(executeCount("select count(?) from tl_post",new Object[]{1}));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        result.setData(wmNews);
        result.setSize(size);
        result.setCode(200);
        result.setCurrentPage(page);

        return result;
    }

    @Override
    public List<TlUser> userList(Long userId) {
        List<TlUser> list = null;
        Object[] params = {userId};
        try {
            list = executeQuery(TlUser.class, "select * from tl_user where userId = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer likeCountAdd(Integer postId, Integer likeCount) {
        Integer list = null;

        Object[] params = {likeCount,postId};
        try {
            list = executeUpdate("update tl_post set likeCount = ? where postId = ?",params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer likeCountDelete(Integer postId, Integer likeCount) {
        Integer list = null;

        Object[] params = {likeCount,postId};
        try {
            list = executeUpdate("update tl_post set likeCount = ? where postId = ?",params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer add(Integer userId, String title, String image, String createDate, String address ,
                       String Copywriting, Long channelId) {
        Integer list = null;

        Object[] params = {userId,title,image,createDate,address,Copywriting,channelId};
        try {
            list = executeUpdate("insert into tl_post(userId,title,image,createDate,address,Copywriting,channelId) " +
                            "values(?,?,?,?,?,?,?)",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public TlUser getUserById(Integer userId) {
        TlUser list = null;

        Object[] params = {userId};
        try {
            list = executeSingleQuery(TlUser.class,"select * from tl_user where userId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public TlPost getPostById(Integer postId) {
        TlPost list = null;

        Object[] params = {postId};
        try {
            list = executeSingleQuery(TlPost.class,"select * from tl_post where postId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer getFlag(Integer userId, Integer userId1) {
        Integer list = null;

        Object[] params = {userId,userId1};
        try {
            list = executeCount("select count(*) from tl_interestid where userId = ? and followId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public List<TlPost> postList(Integer userId) {
        List<TlPost> list = null;
        Object[] params = {userId};
        try {
            list = executeQuery(TlPost.class, "select * from tl_post where userId = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
